﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            //实例化一个Timeys对象
            System.Timers.Timer t = new System.Timers.Timer(1000);
            //获取或设置一个值，该值指示 Timer 是否应引发 Elapsed 事件。
            //表示应该则设为true。
            t.Enabled = true;
            //获取或设置一个布尔值，该值指示 Timer 是否应只引发一次 Elapsed 事件.
            //重复设为true
            t.AutoReset = true;
            //事件注册，Elapsed 事件定义
            t.Elapsed += new System.Timers.ElapsedEventHandler(Tick);
        }
        public void Tick(object source, System.Timers.ElapsedEventArgs e)
        {
            hour1.Text = DateTime.Now.Hour.ToString();//当前时
            minute1.Text = DateTime.Now.Minute.ToString();//当前分
            second1.Text = DateTime.Now.Second.ToString();//当前秒
        }
        public void Alarm(object source, System.Timers.ElapsedEventArgs e)
        {
            string a1 = hour2.Text;//输入闹钟时
            string a2 = minute2.Text;//输入闹钟分
            string a3 = second2.Text;//输入闹钟秒  
            try//异常处理
            {
                if (a1 == "" || a2 == "" || a3 == "")
                {   //调用方法时, 如果至少有一个传递的自变量为null, 则会引发异常
                    throw new ArgumentNullException();
                }
                if (int.Parse(a1) < 0 || int.Parse(a2) < 0 || int.Parse(a3) < 0)
                {
                    throw new myException("时间不能为负值！");
                }
                if (int.Parse(a1) > 23 || int.Parse(a2) > 59 || int.Parse(a3) > 59)
                {
                    throw new myException("时间不能超过相应数值！");
                }
                if (int.Parse(a1) == int.Parse(hour1.Text) && int.Parse(a2) == int.Parse(minute1.Text) && int.Parse(a3) == int.Parse(second1.Text))
                {
                    if (MessageBox.Show("响铃！是否关闭闹钟?选择取消将会在10秒后再次响铃！",
                        " ", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    {
                        //十秒后再次响铃
                        while (true)
                        {
                            if (int.Parse(a3) < 50)
                            {
                                int a4 = int.Parse(a3) + 10;
                                if (int.Parse(a1) == DateTime.Now.Hour &&int.Parse(a2) == DateTime.Now.Minute &a4 == DateTime.Now.Second)
                                {
                                    MessageBox.Show("响铃！是否关闭闹钟？");
                                    break;
                                }
                            }
                            else if (int.Parse(a2) < 59 && int.Parse(a3) >= 50)
                            {
                                int a5 = int.Parse(a2) + 1;
                                int a6 = 10 - (60 - int.Parse(a3));
                                if (int.Parse(a1) == DateTime.Now.Hour && a5 == DateTime.Now.Minute &&a6 == DateTime.Now.Second)
                                {
                                    MessageBox.Show("响铃！是否关闭闹钟？");
                                    break;
                                }
                            }
                            else if (int.Parse(a2) >= 59 && int.Parse(a3) >= 50)
                            {
                                int a7 = int.Parse(a1) + 1;
                                int a8 = 00;
                                int a9 = 10 - (60 - int.Parse(a3));
                                if (a7 == DateTime.Now.Hour &&a8 == DateTime.Now.Minute && a9 == DateTime.Now.Second)
                                {
                                    MessageBox.Show("响铃！是否关闭闹钟？");
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            catch (ArgumentNullException ex)
            {
                t1.Enabled = false;
                t1.Elapsed -= new System.Timers.ElapsedEventHandler(Alarm);
                MessageBox.Show("时间不能为空值！");
            }
            catch (myException ea)
            {
                t1.Enabled = false;
                t1.Elapsed -= new System.Timers.ElapsedEventHandler(Alarm);
                MessageBox.Show(ea.Message);
            }
            catch (FormatException eb)
            {
                t1.Enabled = false;
                t1.Elapsed -= new System.Timers.ElapsedEventHandler(Alarm);
                MessageBox.Show("时间不能为小数！");
            }
        }
        System.Timers.Timer t1 = new System.Timers.Timer(1000);//实例化
        //启动闹钟按钮的点击事件
        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void start_Click(object sender, EventArgs e)
        {
            MessageBox.Show("闹钟启动中！");
            t1.Enabled = true;
            t1.AutoReset = true;
            t1.Elapsed += new System.Timers.ElapsedEventHandler(Alarm);
        }
    }
    class myException : ApplicationException//自定义异常类
    {
        public myException(string message) : base(message)
        {
        }
    }
}
